home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Informant Complete 1995 - 2000
/
Delphi Informant Complete 1995 to 2000.iso
/
Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar
/
1998
/
Nov
/
di9811gd
/
Example4
/
DllCode.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1998-04-25
|
2KB
|
84 lines
unit DllCode;
interface
uses Windows, Dialogs, SysUtils, Forms;
type
TTestObject = class(TObject)
public
ObjectName: String;
constructor Create;
end;
procedure DllShowMessage(Msg: String);
procedure CreateObject;
procedure FreeObject;
var
tlsObjectIndex: DWord;
const
MessageBoxTitle = 'Dll4';
CRLF = #13 + #10;
implementation
procedure DllShowMessage(Msg: String);
begin
MessageBox(GetDesktopWindow, PChar(Msg), MessageBoxTitle,
MB_OK or MB_SETFOREGROUND or MB_TASKMODAL);
end;
procedure CreateObject;
begin
{ First, create the new object and store it in the thread-local
slot referred to by tlsObjectIndex. }
TLSSetValue(tlsObjectIndex, Pointer(TTestObject.Create));
{ Now, get the object that was just created and display
its ObjectName property. }
DllShowMessage(TTestObject(TLSGetValue(tlsObjectIndex)).ObjectName);
end;
procedure FreeObject;
begin
{ First, get the thread-local value and report that it is nil or
give the ObjectName property that was stored there. }
if (TLSGetValue(tlsObjectIndex) = nil) then
DllShowMessage('Object is nil.')
else
DllShowMessage(TTestObject(TLSGetValue(tlsObjectIndex)).ObjectName);
{ Free up the object }
TTestObject(TLSGetValue(tlsObjectIndex)).Free;
{ Set its value in the thread-local store to nil. }
TLSSetValue(tlsObjectIndex, nil);
end;
{ TTestObject }
constructor TTestObject.Create;
begin
ObjectName := 'Test Object: ' + IntToStr(GetCurrentThreadID);
end;
procedure DllEntry(Reason: Integer);
begin
case Reason of
DLL_THREAD_ATTACH: CreateObject;
DLL_THREAD_DETACH: FreeObject;
end;
end;
initialization
IsMultiThread := True;
tlsObjectIndex := TLSAlloc;
DllProc := @DllEntry;
CreateObject;
finalization
FreeObject;
TLSFree(tlsObjectIndex);
end.